package com.yuanfeng.base.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.IndexConfigDTO;
import com.yuanfeng.base.entity.BasePcIndexConfigEntity;
import com.yuanfeng.base.mapper.BasePcIndexConfigMapper;
import com.yuanfeng.base.mapper.BaseSysConfigsMapper;
import com.yuanfeng.base.mapper.WebIndexMapper;
import com.yuanfeng.base.service.BasePcIndexConfigService;
import com.yuanfeng.base.vo.index.*;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.RedisPrefixUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName BasePcIndexConfigServiceImpl
 * @Description
 * @Author cyp
 * @Date 2023-05-24 09:39:23
 * @Version 1.0
 **/
@Service
public class BasePcIndexConfigServiceImpl extends ServiceImpl<BasePcIndexConfigMapper, BasePcIndexConfigEntity>
        implements BasePcIndexConfigService {
    @Autowired
    private WebIndexMapper webIndexMapper;
    @Autowired
    private BaseSysConfigsMapper baseSysConfigsMapper;

    @Value("${system.pcIndexConfig}")
    private String pcIndexConfig;

    private static String pcIndexConfig2;


    @PostConstruct
    public String getPcIndexConfig2() {
        return pcIndexConfig2 = this.pcIndexConfig;
    }

    public Map<Integer, List<PcConfigResultVO>> querygdbPcIndexConfig() {
        List<PcConfigResultVO> result = new ArrayList<>();
        GdbPcIndexConfigVO pcIndexConfig = new GdbPcIndexConfigVO();
        //查询所有数据 解析
        List<GdbPcIndexConfigVO> gdbPcIndexConfigs = this.baseMapper.selectPcIndexConfig(pcIndexConfig);
        if (null != gdbPcIndexConfigs && gdbPcIndexConfigs.size() > 0) {
            for (GdbPcIndexConfigVO gdbPcIndexConfig : gdbPcIndexConfigs) {
                PcConfigResultVO pcConfigResult = new PcConfigResultVO();//用与存放 商品 店铺数据
                pcConfigResult.setPageType(gdbPcIndexConfig.getPageType());
                pcConfigResult.setPageName(gdbPcIndexConfig.getPageName());
                pcConfigResult.setPageTitle(gdbPcIndexConfig.getPageTitle());
                pcConfigResult.setPageTwoTitle(gdbPcIndexConfig.getPageTwoTitle());
                pcConfigResult.setPageStart(gdbPcIndexConfig.getPageStart());
                pcConfigResult.setPageSort(gdbPcIndexConfig.getPageSort());
                pcConfigResult.setPageId(gdbPcIndexConfig.getId());
                String data = gdbPcIndexConfig.getPageData();// 获取页面数据
                if (StrUtil.isNotBlank(data)) {
                    GdbPcIndexConfigDataVO gdbPcIndexConfigData = JSON.parseObject(data, GdbPcIndexConfigDataVO.class);
                    List<Integer> ids = gdbPcIndexConfigData.getData();
                    if (null != gdbPcIndexConfigData.getType() && 1 == gdbPcIndexConfigData.getType()) {//商品
                        List<PcConfigGoodsVO> goods = new ArrayList<>();//存放商品信息
                        if (null != ids) {
                            for (Integer id : ids) {
                                PcConfigGoodsVO pcConfigGoodsParam = webIndexMapper.pcConfigGoods(id);//根据common 查询商品信息
                                if (null != pcConfigGoodsParam) {
                                    goods.add(pcConfigGoodsParam);
                                }
                            }
                            pcConfigResult.setGoods(goods);
                        }
                    } else if (null != gdbPcIndexConfigData.getType() && 2 == gdbPcIndexConfigData.getType()) {//店铺
                        List<PcConfigShopVO> shops = new ArrayList<>();//存放店铺信息
                        if (null != ids) {
                            for (Integer id : ids) {
                                PcConfigShopVO pcConfigShopParam = webIndexMapper.pcConfigShop(id);//根据shopid
                                if (null != pcConfigShopParam) {
                                    if (StringUtils.isEmpty(pcConfigShopParam.getCommonImage())) {
                                        String icoShop = baseSysConfigsMapper.selectConfigValue("logo_copywriting_ico_shop");
                                        pcConfigShopParam.setCommonImage(icoShop);
                                    }
                                    shops.add(pcConfigShopParam);
                                }
                            }
                            pcConfigResult.setShops(shops);
                        }
                    } else if (null != gdbPcIndexConfigData.getType() && 3 == gdbPcIndexConfigData.getType()) {//分类
                        PcConfigCatVO pcConfigCatParam = new PcConfigCatVO();
                        pcConfigCatParam.setCatImage(gdbPcIndexConfigData.getImages());
                        if (null != ids) {
                            for (Integer id : ids) {
                                pcConfigCatParam.setCatUrl(String.valueOf(id));
                            }
                        }
                        pcConfigResult.setCat(pcConfigCatParam);
                    }
                }
                result.add(pcConfigResult);
            }
        }
        //对数据库中数据分组 返回
        return result.stream().collect(Collectors.groupingBy(PcConfigResultVO::getPageType));
    }

    @Override
    public ResponseResult updatePcIndexConfig(IndexConfigDTO pcIndexConfig) {
        if (null != pcIndexConfig) {
            //根据id 判断是否是添加
            if (null != pcIndexConfig.getId()) {
                //修改
                this.baseMapper.update(null, new LambdaUpdateWrapper<BasePcIndexConfigEntity>()
                        .eq(BasePcIndexConfigEntity::getId, pcIndexConfig.getId())
                        .set(BasePcIndexConfigEntity::getPageName, pcIndexConfig.getPageName())
                        .set(BasePcIndexConfigEntity::getPageType, pcIndexConfig.getPageType())
                        .set(BasePcIndexConfigEntity::getPageTitle, pcIndexConfig.getPageTitle())
                        .set(BasePcIndexConfigEntity::getPageTwoTitle, pcIndexConfig.getPageTwoTitle())
                        .set(BasePcIndexConfigEntity::getPageData, pcIndexConfig.getPageData())
                        .set(BasePcIndexConfigEntity::getCreateTime, new Date())
                );
            } else {
                BasePcIndexConfigEntity entity = new BasePcIndexConfigEntity();
                BeanUtils.copyProperties(pcIndexConfig, entity);
                this.baseMapper.insert(entity);
            }
            //删除redis缓存
            RedisUtil.removeAll(RedisPrefixUtils.pcIndexConfig);
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.MISSING_PARAMETER);
    }
}

